/**
  * This file is part of Medusa.
  *
  * Authors: Uriel Fanelli <uriel.fanelli@gmail.com>
  *          Perthect <perthect@gmail.com>
  *          Marco Bizzarri <Marco.Bizzarri@gmail.com>
  *
  *
  * Medusa is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  * the Free Software Foundation; either version 2 of the License, or
  * (at your option) any later version.

  * Medusa is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.

  * You should have received a copy of the GNU General Public License
  * along with Medusa; if not, write to the Free Software
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  *
  */

#include "goongress.h"

#include "proxy_settings.h"
#include "login.h"
#include "proxy_dialog.h"

#include "gprogressbar.h"
#include "singletab.h"
#include "tabwidget.h"

QTgoongress::QTgoongress()
{

   // creiamo un proxy per l'applicazione
   handleProxyConfig app_proxy;
   app_proxy.deployProxy();

   this->popola();

//    this->progressBar = new GProgressBar;
    xprogressBar = new GProgressBar;

///////////////////////////////////////////////////////////////////////////////////////
     setWindowIcon(QIcon(":/resources/icons/qtasterisk.png"));
     //showMaximized();
     setWindowTitle("Medusa");
     createActions(); // respect order > 1A 2M 3TB 4SB
     createMenus();
     createToolBars();
     createStatusBar();
     disegna();
     login();
///////////////////////////////////////////////////////////////////////////////////////

}

tabWidget *QTgoongress::tWidget() const
{
    return newqtab;
}

GProgressBar *QTgoongress::pBar() const
{
    return xprogressBar;
}

 void QTgoongress::createToolBars()
 {
    startToolBar = addToolBar(tr("Start"));
    startToolBar->addAction(loginAct);
    startToolBar->addAction(proxyAct);
    startToolBar->addAction(exitAct);
    driveToolBar = addToolBar(tr("Drive"));
    driveToolBar->addAction(reloadAct);
    driveToolBar->addAction(backAct);
    driveToolBar->addAction(forwardAct);
    driveToolBar->addAction(stoploadAct);
    startToolBar->addAction(filebroAct);
    helpToolBar = addToolBar(tr("Help"));
    helpToolBar->addAction(searchAct);
    helpToolBar->addAction(indexAct);
    helpToolBar->addAction(aboutAct);
    addToolBar(Qt::LeftToolBarArea,startToolBar);
    addToolBar(Qt::LeftToolBarArea,helpToolBar);
    addToolBar(Qt::RightToolBarArea,driveToolBar);
 }

 void QTgoongress::createStatusBar()
 {
     stbar = new QTAstbar();
     setStatusBar(stbar);
 }

void QTgoongress::vai_a(QTreeWidgetItem *u, int column)
{
    std::string p = u->text(column).toStdString();
    std::string w = u->text(1).toStdString();
    if (w.length() > 0) {
///////////////////////////////////////////////////////////////////////////////////////
     newqtab->aggiungitab(QUrl(u->text(1)),QIcon(u->icon(0)),u->text(column));
     newqtab->qsb->setViewName((QUrl(u->text(1))).toString());
///////////////////////////////////////////////////////////////////////////////////////
    }
}

QSizePolicy QTgoongress::makePolicy() {
     QSizePolicy sizePolicy(QSizePolicy::Fixed, QSizePolicy::Expanding);
     sizePolicy.setHorizontalStretch(0);
     sizePolicy.setVerticalStretch(5);
     return sizePolicy;
}

QHBoxLayout *QTgoongress::makeMenuBar() {
     loginAct = new QAction(tr("&Login..."), this);
     connect(loginAct, SIGNAL(triggered()), this, SLOT(login()));

     proxyAct = new QAction(tr("&Proxy"), this);
     quitAct = new QAction(tr("&Exit"), this);
     quitAct->setShortcuts(QKeySequence::Close);
     quitAct->setStatusTip(tr("Exit QT-asterisk"));
     connect(quitAct, SIGNAL(triggered()), this, SLOT(exit()));

     reloadAct = new QAction(tr("&Reload"), this);
     backAct = new QAction(tr("&Back"), this);
     forwardAct = new QAction(tr("&Forward"), this);
     stoploadAct = new QAction(tr("&Stop Load"), this);
     filebroAct = new QAction(tr("&File Browse"), this);

     searchAct = new QAction(tr("&Search"), this);
     indexAct = new QAction(tr("&Index"), this);
     aboutAct = new QAction(tr("&Medusa..."), this);
     connect(aboutAct, SIGNAL(triggered()), this, SLOT(about()));

     QMenuBar *qm = new QMenuBar(this);

     QMenu *start = qm->addMenu("&Start");
     start->addAction(loginAct);
     start->addAction(proxyAct);
     start->addSeparator();
     start->addAction(quitAct);

     QMenu *start1 = qm->addMenu("&Drive");
     start1->addAction(reloadAct);
     start1->addAction(backAct);
     start1->addAction(forwardAct);
     start1->addAction(stoploadAct);
     start1->addAction(filebroAct);

     QMenu *start2 = qm->addMenu("&Help");
     start2->addAction(searchAct);
     start2->addAction(indexAct);
     start2->addSeparator();
     start2->addAction(aboutAct);

     QHBoxLayout *bottoni = new QHBoxLayout;
     bottoni->setAlignment(Qt::AlignLeft);
     bottoni->addWidget(qm);
     return bottoni;
}

QHBoxLayout *QTgoongress::makeTwoColumns()
{
    QHBoxLayout *affiancati = new QHBoxLayout;
    affiancati->addWidget(&box);
    affiancati->addWidget(newqtab);
    return affiancati;
}
QHBoxLayout *QTgoongress::makePBar()
{
    QHBoxLayout *mtbar = new QHBoxLayout;
//    xprogressBar = new GProgressBar;
    mtbar->addWidget(xprogressBar);
    //xprogressBar->hide();
    return mtbar;
}

QVBoxLayout *QTgoongress::makeMainLayout()
{
     QVBoxLayout *impilati = new QVBoxLayout;
     impilati->addLayout(makeTwoColumns());
     impilati->addLayout(makePBar());
     return impilati;
}

void QTgoongress::disegna()
{
    box.setColumnCount(1);
    box.setHeaderLabel("My Google");
    box.setMouseTracking(true);
    box.setObjectName("albero");
    box.setSizePolicy(makePolicy());
///////////////////////////////////////////////////////////////////////////////////////
    newqtab = new tabWidget(xprogressBar,stbar);
///////////////////////////////////////////////////////////////////////////////////////
    connect( &box, SIGNAL(itemPressed(QTreeWidgetItem*,int)), this, SLOT( vai_a(QTreeWidgetItem *, int) ));
    QFrame *frame = new QFrame;
    frame->setLayout(makeMainLayout());
    setCentralWidget(frame);
}

void QTgoongress::pls()
 {
    progressBar->pageLoadStarted();
 }
void QTgoongress::plip(int i)
 {
    progressBar->pageLoadInProgress(i);
 }
void QTgoongress::plf(int i)
 {
    progressBar->pageLoadFinished(i);
 }

void QTgoongress::about()
{
    newqtab->aggiungitab(QUrl("http://code.google.com/p/qt-asterisk/"),QIcon(":/resources/icons/qtasterisk.png"),tr("&Medusa"));
}

void QTgoongress::login()
{
    newqtab->aggiungitab(QUrl("https://www.google.com/accounts/ServiceLogin"),QIcon(":/resources/icons/login.png"),tr("&Login"));
}

void QTgoongress::proxy()
{
    dialogProxyConfig *procsi = new dialogProxyConfig;
}

void QTgoongress::quit()
{
    exit(0);
}

void QTgoongress::pagereload()
{
    newqtab->aggiornatab(-8888);
}

void QTgoongress::pageback()
{
    newqtab->pageback(-8888);
}

void QTgoongress::pageforward()
{
    newqtab->pageforward(-8888);
}

void QTgoongress::stopload()
{
    newqtab->stopload(-8888);
}

void QTgoongress::filebrowse()
{
    QFileDialog qfd;
    QString fileName = qfd.getOpenFileName(this,
                                           tr("Browse Files"),
                                           QDir::homePath(),
                                           tr("All Files (*.*)")); // CHECKITOUT
    if (!fileName.isEmpty()){
//        loadFile(fileName);
    }
}

void QTgoongress::support()
{
    newqtab->aggiungitab(QUrl("http://code.google.com/p/qt-asterisk/issues/list"),QIcon(":/resources/icons/hint.png"),tr("&Get Support"));
}

void QTgoongress::search()
{
    newqtab->aggiungitab(QUrl("http://code.google.com/p/qt-asterisk/updates/list"),QIcon(":/resources/icons/index.png"),tr("&News"));
}

///////////////////////////////////////////////////////////////////////////////////////
void QTgoongress::createActions()
{
     loginAct = new QAction(QIcon(":/resources/icons/login.png"), tr("&Login..."), this);
     loginAct->setStatusTip(tr("Login"));
     connect(loginAct, SIGNAL(triggered()), this, SLOT(login()));

     proxyAct = new QAction(QIcon(":/resources/icons/proxy.png"), tr("&Proxy..."), this);
     proxyAct->setStatusTip(tr("Proxy"));
     connect(proxyAct, SIGNAL(triggered()), this, SLOT(proxy()));
     exitAct = new QAction(QIcon(":/resources/icons/exit.png"), tr("&Exit"), this);
     exitAct->setShortcut(tr("Ctrl+Q"));
     exitAct->setStatusTip(tr("Exit"));
     connect(exitAct, SIGNAL(triggered()), this, SLOT(quit()));

     reloadAct = new QAction(QIcon(":/resources/icons/reload.png"), tr("&Reload"), this);
     reloadAct->setStatusTip(tr("Reload"));
     connect(reloadAct, SIGNAL(triggered()), this, SLOT(pagereload()));
     backAct = new QAction(QIcon(":/resources/icons/rewind.png"), tr("&Back"), this);
     backAct->setStatusTip(tr("Back"));
     connect(backAct, SIGNAL(triggered()), this, SLOT(pageback()));
     forwardAct = new QAction(QIcon(":/resources/icons/forward.png"), tr("&Forward"), this);
     forwardAct->setStatusTip(tr("Forward"));
     connect(forwardAct, SIGNAL(triggered()), this, SLOT(pageforward()));
     stoploadAct = new QAction(QIcon(":/resources/icons/stop.png"), tr("&Stop Load"), this);
     stoploadAct->setStatusTip(tr("Stop Load"));
     connect(stoploadAct, SIGNAL(triggered()), this, SLOT(stopload()));
     filebroAct = new QAction(QIcon(":/resources/icons/download.png"), tr("&File Browse"), this);
     filebroAct->setStatusTip(tr("File Browse"));
     connect(filebroAct, SIGNAL(triggered()), this, SLOT(filebrowse()));

     searchAct = new QAction(QIcon(":/resources/icons/index.png"), tr("&News"), this);
     searchAct->setStatusTip(tr("News"));
     connect(searchAct, SIGNAL(triggered()), this, SLOT(search()));
     indexAct = new QAction(QIcon(":/resources/icons/hint.png"), tr("&Support"), this);
     indexAct->setStatusTip(tr("Get Support"));
     connect(indexAct, SIGNAL(triggered()), this, SLOT(support()));
     aboutAct = new QAction(QIcon(":/resources/icons/qtasterisk.png"), tr("&Medusa..."), this);
     aboutAct->setStatusTip(tr("About Medusa"));
     connect(aboutAct, SIGNAL(triggered()), this, SLOT(about()));
}

void QTgoongress::createMenus()
{
     fileMenu = menuBar()->addMenu(tr("&Start"));
     fileMenu->addAction(loginAct);

     fileMenu->addAction(proxyAct);
     fileMenu->addSeparator();
     fileMenu->addAction(exitAct);
     menuBar()->addSeparator();


     driveMenu = menuBar()->addMenu(tr("&Drive"));
     driveMenu->addAction(reloadAct);
     driveMenu->addAction(backAct);
     driveMenu->addAction(forwardAct);
     driveMenu->addAction(stoploadAct);
     driveMenu->addAction(filebroAct);

     helpMenu = menuBar()->addMenu(tr("&Help"));
     helpMenu->addAction(searchAct);
     helpMenu->addAction(indexAct);
     helpMenu->addSeparator();
     helpMenu->addAction(aboutAct);
}
